package com.yjd.back.funds;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.fundservice.model.FundChannelModel;
import com.yjd.comm.fundservice.service.IFundChannelService;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.PagerUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yangchangyan on 2018/3/8.
 */
@Controller
@Scope("prototype")
@RequestMapping("/funds/channel")
public class FundsChannelControl extends BaseControl {

    @Resource
    private IFundChannelService fundChannelService;

    private static int maxFundChannelId = 0;

    /**
     * 资金渠道列表
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "fund_channel asc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.fundChannelService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/funds/channel/list"));

        List<FundChannelModel> fundChannelModelList = (List<FundChannelModel>) pager.getPagerModel().getDataList();

        for (FundChannelModel model : fundChannelModelList){
            maxFundChannelId = model.getFund_channel();
            model.getStrMap().put("create_time", DateUtil.timestampToDateString(model.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            model.getStrMap().put("update_time", DateUtil.timestampToDateString(model.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("pager", pager);
        return "funds.channel.list";
    }

    /**
     * 资金渠道详情
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/detail")
    public String detail(int id) throws Exception {

        FundChannelModel channel = this.fundChannelService.getModelById(id, DbWREnums.READ);

        request.setAttribute("channel", channel);
        return "funds.channel.detail";
    }

    /**
     * 资金渠道编辑
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/edit")
    public String edit(int id) throws Exception {

        FundChannelModel channel = this.fundChannelService.getModelById(id, DbWREnums.READ);

        request.setAttribute("channel", channel);
        return "funds.channel.edit";
    }

    /**
     * 资金渠道修改
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/submit")
    public void save(FundChannelModel channel) throws Exception {
        if(null == channel){
            FrameUtil.throwBaseException1("信息为空");
        }

        long currentTimeInSecond = FrameUtil.getTime();
        // 修改
        if(channel.getFund_channel() > 0){
            FundChannelModel dbChannel = this.fundChannelService.getModelById(channel.getFund_channel(), DbWREnums.READ);
            if(null == dbChannel){
                FrameUtil.throwBaseException1("渠道不存在");
            }

            channel.setWhere_version(dbChannel.getVersion());
            channel.setUpdate_time(currentTimeInSecond);
            this.fundChannelService.update(channel);
        }
        // 新增
        else {
            channel.setFund_channel(maxFundChannelId + 1);
            channel.setCreate_time(currentTimeInSecond);
            channel.setUpdate_time(currentTimeInSecond);
            this.fundChannelService.insert(channel);
        }

        super.successMsg(ServletUtil.getUrl("/funds/channel/list"), "保存成功");
    }

    /**
     * 资金渠道新增
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add")
    public String add() throws Exception {
        request.setAttribute("channel", new FundChannelModel());
        return "funds.channel.detail";
    }

    /**
     * 资金渠道删除
     *
     * @return void
     * @throws
     */
    @RequestMapping(value = "/delete")
    public void delete(int id) throws Exception {
        this.fundChannelService.delete(id);
        super.successMsg(ServletUtil.getUrl("/funds/channel/list"), "删除成功");
    }
}
